package cn.ml.duoXianCheng.qiangPiao;

public class QiangPiao1 implements Runnable{
    private int count=10;  //记录剩余票数
    private int num = 0;   //记录买到第几张票
//    private String name;
//    public QiangPiao1(String name) {
//        this.name = name;
//    }
    public void  run(){
        while(true) {
            synchronized ("o") {
                //没有余票时，跳出循环
                if (count <= 0) {
                    break;
                }
                //第一步：修改数据
                num++;
                count--;
                try {
                    Thread.sleep(500); //模拟网络延时
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //第二步：显示信息
                System.out.println(Thread.currentThread().getName()  + "抢到第" + num + "张票，剩余" + count + "张票！");

            }


        }
    }

    public static void main(String[] args) {
        QiangPiao1 site = new QiangPiao1();
        Thread person1= new Thread(site,"张三三");
        Thread person2= new Thread(site,"李四四");
        Thread person3= new Thread(site,"王五五");
        System.out.println("********开始抢票********");
        person1.start();
        person2.start();
        person3.start();
    }

}
